//
//  ForgetPasswordViewController.m
//  BabyAndBabyDriver
//
//  Created by 尹宁 on 16/8/26.
//  Copyright © 2016年 范琦. All rights reserved.
//

#import "ForgetPasswordViewController.h"
#import "forgetPasswordView.h"
#import "UIViewController+HUD.h"
#import "MBProgressHUD.h"
@interface ForgetPasswordViewController ()
{
    
    MBProgressHUD *hud;
}
@property (nonatomic, strong) forgetPasswordView *forgetPasswordView;
@end

@implementation ForgetPasswordViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    self.title = @"找回密码";
    self.view.backgroundColor = [UIColor whiteColor];
    [self creatforgetPasswordView];
}

- (void)creatforgetPasswordView
{
    self.forgetPasswordView = [[[NSBundle mainBundle] loadNibNamed:@"forgetPasswordView" owner:nil options:nil] lastObject];
    _forgetPasswordView.frame = CGRectMake(0, 0, KSCREENWIDTH, KSCREENHEIGHT);
    _forgetPasswordView.firstView.layer.borderWidth = 0.5;
    _forgetPasswordView.firstView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    _forgetPasswordView.firstView.layer.masksToBounds = YES;
    _forgetPasswordView.firstView.layer.cornerRadius = 5;
    
    _forgetPasswordView.secondView.layer.borderWidth = 0.5;
    _forgetPasswordView.secondView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    _forgetPasswordView.secondView.layer.masksToBounds = YES;
    _forgetPasswordView.secondView.layer.cornerRadius = 5;
    
    _forgetPasswordView.thirdView.layer.borderWidth = 0.5;
    _forgetPasswordView.thirdView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    _forgetPasswordView.thirdView.layer.masksToBounds = YES;
    _forgetPasswordView.thirdView.layer.cornerRadius = 5;
    
    _forgetPasswordView.lastView.layer.borderWidth = 0.5;
    _forgetPasswordView.lastView.layer.borderColor = [UIColor lightGrayColor].CGColor;
    _forgetPasswordView.lastView.layer.masksToBounds = YES;
    _forgetPasswordView.lastView.layer.cornerRadius = 5;
    [self.view addSubview:_forgetPasswordView];
    
    [[_forgetPasswordView.getCodeButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
        if (_forgetPasswordView.phoneNumberTextfield.text.length == 0 || _forgetPasswordView.phoneNumberTextfield.text.length != 11) {
            [self showHint:@"请填写正确手机号" yOffset:-300];
        } else {
            [self getCode];
        }
    }];
    
    [[_forgetPasswordView.makesureButton rac_signalForControlEvents:UIControlEventTouchUpInside] subscribeNext:^(id x) {
 
        if (_forgetPasswordView.phoneNumberTextfield.text.length == 0 || _forgetPasswordView.phoneNumberTextfield.text.length != 11) {
            [self showHint:@"请填写正确手机号" yOffset:-300];
        } else if (_forgetPasswordView.codeTextfield.text.length == 0) {
            [self showHint:@"请填写验证码" yOffset:-300];
        } else if (_forgetPasswordView.passwordTextfield.text.length == 0) {
            [self showHint:@"请填写密码" yOffset:-300];
        } else if (_forgetPasswordView.passwordTextfield.text.length != _forgetPasswordView.makesurePasswordField.text.length) {
            [self showHint:@"请确认两次密码是否相同" yOffset:-300];
        } else {
            [self makesureButton];
        }
        
    }];
}

- (void)makesureButton
{
    YTKNetHandle *api = [[YTKNetHandle alloc]initWithUrl:@"Auth/forget" paramDic:@{@"username":_forgetPasswordView.phoneNumberTextfield.text, @"password":_forgetPasswordView.passwordTextfield.text,@"smscode":_forgetPasswordView.codeTextfield.text} RegisterApiMethod:RequestMethodPost];
    
    [api startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
        NSLog(@"%@", request.responseBody);
        [hud hide:YES];
     
    } failure:^(YTKBaseRequest *request) {
    }];
    
}

- (void)getCode
{
    
        YTKNetHandle *api = [[YTKNetHandle alloc]initWithUrl:@"Auth/sendSms" paramDic:@{@"mobile":_forgetPasswordView.phoneNumberTextfield.text} RegisterApiMethod:RequestMethodPost];
        [api startWithCompletionBlockWithSuccess:^(YTKBaseRequest *request) {
            NSLog(@"%@", request.responseBody);
            
            if ([request.responseBody[@"error"] integerValue] == 0) {
                
                
                
            }else{
                
                
//                [self showAlertView:request.responseBody[@"msg"] timeOut:1];
            }
        } failure:^(YTKBaseRequest *request) {
        }];

}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
